﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using SdmxMl.Common;
using SdmxMl.Manager;
using SdmxMl.Structure;
using SdmxMl.GenericMetadata;


namespace SilcQuest.CSV
{
    /// <summary>
    /// Helport to export part of history tracing in csv
    /// </summary>
    public class ExportCsvSuivi : CsvExportBase
    {
        private DateTime dtFrom;
        private DateTime dtTo;
        private StreamWriter sw;
        private SmManager sm;
        protected RefVarHelper varHelper;

        public ExportCsvSuivi(SmManager smgr, string path, char delimiter, DateTime dtFrom, DateTime dtTo) : base (delimiter)
        {
            sm = smgr;
            varHelper = new RefVarHelper(sm);
            this.dtFrom = dtFrom;
            this.dtTo = dtTo;
            sw = new StreamWriter(path, false, Encoding.Default);
        }

        public void Close()
        {
            sw.Flush();
            sw.Close();
        }

        public void WriteQuestHistory(MetadataSetType metaSet, bool writeHeader)
        {
            MetadataStructureDefinitionType msd = sm.GetArtefact(SdmxArtefactType.MetadataStructureDefinitions, metaSet.MetadataStructureRef, true) as MetadataStructureDefinitionType;
            ReportStructureType report = msd.ReportStructureList.LocateItem(metaSet.ReportRef);
            int keyCount = msd.TargetIdentifiers.FullTargetIdentifier.IdentifierComponentList.Count;
            int count = keyCount + report.MetadataAttributeList.Count;
            int col = 1;
            if (writeHeader)
            {
                foreach (IdentifierComponentType key in msd.TargetIdentifiers.FullTargetIdentifier.IdentifierComponentList)
                    AddCell(sw, col++, count, key.Id, true);
                foreach (MetadataAttributeType meta in report.MetadataAttributeList)
                    AddCell(sw, col++, count, meta.ConceptInfo.Id, true);
            }

            foreach (AttributeValueSetType atr in metaSet.AttributeValueSetList)
            {
                // is in Date range
                DateTime dtAnnot = Convert.ToDateTime(atr.TargetValuesTypeList.GetComponentValue("DT").SimpleValue);
                if (dtAnnot >= dtFrom && dtAnnot <= dtTo)
                {
                    // write annotation content
                    col = 1;
                    foreach (IdentifierComponentType key in msd.TargetIdentifiers.FullTargetIdentifier.IdentifierComponentList)
                        AddCell(sw, col++, count, atr.TargetValuesTypeList[key.Id], true);

                    // Meta part, transmit code & code text
                    
                    foreach (MetadataAttributeType meta in report.MetadataAttributeList)
                    {
                        ReportedAttributeType ra = atr.ReportedAttributeList.GetReportedAttributeOfConcept(meta.ConceptInfo.Id);
                        string val = string.Empty;
                        if (ra != null)
                        {
                            val = ra.InternationalizedValue.ToString();
                            CodeListType clt = varHelper.GetCodelistOfMetaAtt(meta);
                            if (clt != null)
                            {
                                ISdmxHierarchyListItem item = clt.CodeList.LocateItem(val);
                                if (item != null)
                                    val = item.ToString();
                            }
                        }

                        AddCell(sw, col++, count, val, true);
                    }

                }
            }
        }

    }
}
